<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;
use think\Db;

/**
 * Template控制器
 */
class Template extends AdminBase
{
    
    /**
     * Template列表
     */
    public function TemplateList()
    {
        
        $where = $this->logicTemplate->getWhere($this->param);
        
        $this->assign('list', $this->logicTemplate->getTemplateList($where, 'a.*', 'a.create_time desc'));
        
        return $this->fetch('template_list');
    }
    
    /**
     * Template添加
     */
    public function TemplateAdd()
    {
        
        $this->TemplateCommon();
        $this->assign('type',$this->param['type']);
        switch ($this->param['type']){
            case 1:
                return $this->fetch('user_template_edit');
                break;
            case 2:
                return $this->fetch('user_template_edit1');
                break;
            case 3:
                return $this->fetch('user_template_edit2');
                break;
            case 4:
                return $this->fetch('user_template_edit3');
                break;
        }


    }
    
    /**
     * Template编辑
     */
    public function TemplateEdit()
    {
        
        $this->TemplateCommon();
        
        $info = $this->logicTemplate->getTemplateInfo(['a.id' => $this->param['id']], 'a.*');

        
        $this->assign('info', $info);
        $this->assign('type', $info['type']);
        switch ($info['type']){
            case 1:
                return $this->fetch('user_template_edit');
                break;
            case 2:
                return $this->fetch('user_template_edit1');
                break;
            case 3:
                return $this->fetch('user_template_edit2');
                break;
            default:
                return $this->fetch('user_template_edit3');
                break;
        }
    }
    
    /**
     * Template添加与编辑通用方法
     */
    public function TemplateCommon()
    {
        
        IS_POST && $this->jump($this->logicTemplate->TemplateEdit($this->param));
        

    }
    /**
     * Template执行方法
     */
    public function implement()
    {

        $this->jump($this->logicTemplate->implement($this->param));


    }
    public function templatedel()
    {

        $this->jump($this->logicAdminBase->setStatus('Template', $this->param));
    }
    /**
     * Template分类添加
     */
    public function TemplateCategoryAdd()
    {
        
        IS_POST && $this->jump($this->logicTemplate->TemplateCategoryEdit($this->param));
        
        return $this->fetch('Template_category_edit');
    }
    
    /**
     * Template分类编辑
     */
    public function TemplateCategoryEdit()
    {
        
        IS_POST && $this->jump($this->logicTemplate->TemplateCategoryEdit($this->param));
        
        $info = $this->logicTemplate->getTemplateCategoryInfo(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        
        return $this->fetch('Template_category_edit');
    }
    
    /**
     * Template分类列表
     */
    public function TemplateCategoryList()
    {
        
        $this->assign('list', $this->logicTemplate->getTemplateCategoryList());
       
        return $this->fetch('Template_category_list');
    }
    
    /**
     * Template分类删除
     */
    public function TemplateCategoryDel($id = 0)
    {
        
        $this->jump($this->logicTemplate->TemplateCategoryDel(['id' => $id]));
    }
    
    /**
     * 数据状态设置
     */
    public function setStatus()
    {
        
        $this->jump($this->logicAdminBase->setStatus('Template', $this->param));
    }

    public function copy_template(){
        if(empty($_POST)){
            $id = $_GET['id'];
            $this->assign('id',$id);
            return $this->fetch();
        }else{
            $id = input('id');
            $templdate_name = input('template_name');
            $old_record = Db::name("template")->where(["id"=>$id])->find();
            $data = array(
                "template_name" => $templdate_name,
                "type" => $old_record['type'],
                "user_number" => 0,
                "remark" => $old_record["remark"],
                "content" => $old_record["content"],
                "create_time" => time(),
                'status' => 1,
                'use_count' => 0,
                'update_time' => time()
            );
            Db::name("template")->insert($data);
            $this->success("复制成功");
        }
    }
}
